// 创建模块
var app = angular.module('starterApp', 
    ['ngSanitize', 'ngAnimate', 'ngRoute', 'starter.essay.controllers', 
      'starter.controllers', 'starter.services', 
      'starter.filters', 'starter.directives']);


// 配置路由
app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {  
    $routeProvider          
        .when('/list/:sourceType?', {
            templateUrl: '/templates/essay_list.html',
            controller: "listEssayCtl"
        })
        .when('/operate-essay/:sourceType/:id?', {
            templateUrl: '/templates/essay_operate.html',
            controller: "operateEssayCtl"
        })
        .when('/operate-essay-manage-tag/:sourceType?', {
            templateUrl: '/templates/essay_operate_tag.html',
            controller: "operateEssayCtl"
        })
        .when('/operate-tag/:sourceType/:id?', {
            templateUrl: '/templates/tag_operate.html',
            controller: "operateTagCtl"
        }).otherwise({redirectTo: '/list'});

    //$locationProvider.html5Mode(true);

}]);

app.run(['$rootScope', '$window', '$location', '$log', function ($rootScope, $window, $location, $log) {  
    var locationChangeStartOff = $rootScope.$on('$locationChangeStart', locationChangeStart);  
    var locationChangeSuccessOff = $rootScope.$on('$locationChangeSuccess', locationChangeSuccess);  
  
    var routeChangeStartOff = $rootScope.$on('$routeChangeStart', routeChangeStart);  
    var routeChangeSuccessOff = $rootScope.$on('$routeChangeSuccess', routeChangeSuccess);  
  
    function locationChangeStart(event) {  
        //$log.log('locationChangeStart');  
        //$log.log(arguments);  


        locationChangeStartOff(); //Stop listening for location changes or you can do others  
        $location.path("/");  
        return;  

    }  
  
    function locationChangeSuccess(event) {  
        //$log.log('locationChangeSuccess');  
        //$log.log(arguments);  
    }  
  
    function routeChangeStart(event) {  
        //$log.log('routeChangeStart'); 
        //$log.log(arguments);  
        //App.blockUI({target: $(".show-type-list"), iconOnly: true});
    }  
  
    function routeChangeSuccess(event) {  
        //$log.log('routeChangeSuccess');  
        //$log.log(arguments);  
        //App.unblockUI($(".show-type-list"));
    }  
}]);

angular.module('starter.essay.controllers', [])
.controller('starterCtl', ['$scope', '$routeParams', function($scope, $routeParams){
    //sourceType 1 from main, 2 from cancel create 
    $scope.source_list_type = $routeParams.sourceType || 1;
            
    $scope.source = {
        showCancelCreate: false,
        curEssay: {},
        tagList:[],
        categoryList: [],
        docList:[],
        total: 0,
        condition: {
            searchCriteria:{
                status: "1, 2"
            },
            pagingMetaData: {
                pageIndex: 0
            },
            sortMetaData: [{
                column: "",
                isAscending:false
            }]
        }
    };
    $scope.base = common.utils.cloneObj($scope.source);

    $scope.callback_submitEssay = function(tag){
        location.href = "#/list/1";
    };

    $scope.callback_submitTag = function(tag){
        $scope.source.tagList.push(tag);
        location.href = "#/operate-essay-manage-tag/2";
    };

    $scope.init_source = function(){
        $scope.source = common.utils.cloneObj($scope.base);
    }
    $scope.$on('$viewContentLoaded', function() {  
    });  
}])

.controller('listEssayCtl', ['$scope', 'EssayService', '$routeParams', '$location', function($scope, EssayService, $routeParams, $location){
    //sourceType 1 from main, 2 from cancel create 
    $scope.source_list_type = $routeParams.sourceType || 1;
    
    $scope.selectedList = [];
    $scope.total = 0;
    
    console.log($location);
        
    $scope.status = {
        busy: false,
        done: false,
        complete: false
    };
    $scope.search = function(isDo){
        if($scope.status.busy) return;
        $scope.status.busy = true;

        if($scope.source_list_type == 1 || !!isDo){
            var promise = EssayService.getEssayList($scope.source.condition);
            promise.then(
                function(response){
                    if(response.data.status != 1){
                        art.tip.danger("has error when page get essay list.");
                    }
                    else{
                        jQuery.merge($scope.source.docList, response.data.result)
                        
                        $scope.docList = $scope.source.docList;
                        $scope.source.total = response.data.total;

                        $scope.status.busy = false;
                        $scope.status.done = true;
                        $scope.status.complete = $scope.source.total <= $scope.docList.length;

                        if(!$scope.status.complete){
                            $scope.source.condition.pagingMetaData.pageIndex++;
                        }
                    }
                },
                function (error) {
                    art.tip.danger("service error status: " + error.status);
                    $scope.docList = [];
                    $scope.source.docList = [];

                    $scope.status.busy = false;
                    $scope.status.done = false;
                    $scope.status.complete = $scope.source.total <= $scope.docList.length;
                }
            );
        }
        else{
            $scope.docList = $scope.source.docList;

            $scope.status.busy = false;
            $scope.status.done = true;
            $scope.status.complete = $scope.source.total <= $scope.docList.length;
        }
    };

    $scope.setTagBg = function(color){
        return {'background-color': color};
    };

    $scope.$on('$viewContentLoaded', function() {
        if($scope.source_list_type == 1){
            $scope.init_source();
        }        

        $scope.source.showCancelCreate = false;
        $scope.search();
    });  
}])
